﻿using System.Collections.Generic;
using QFramework;
using UnityEngine;

namespace HareGame
{
    [MonoSingletonPath("GameEntry/UIManager")]
    public class UIManager : MonoBehaviour, ISingleton
    {
        public List<UIBase> AllPopUI = new List<UIBase>();
        /// <summary>
        /// 初始化
        /// </summary>
        public void OnSingletonInit()
        {

        }

        /// <summary>
        /// 定义单例
        /// </summary>
        public static UIManager Instance
        {
            get { return MonoSingletonProperty<UIManager>.Instance; }
        }

        /// <summary>
        /// 打开UI
        /// </summary>
        /// <typeparam name="T">UIType</typeparam>
        /// <param name="level">层级</param>
        /// <param name="uiData">数据</param>
        public T OpenUI<T>(UILevel level, IUIData uiData = null) where T : UIBase
        {
            UIBase ui = UIKit.OpenPanel<T>(level, uiData);
            if (level == UILevel.PopUI)
            {
                if (AllPopUI.Contains(ui))
                    LogKit.E("重复打开了UI:" + ui);
                else
                    AllPopUI.Add(ui);
            }
            return (T)ui;
        }

        /// <summary>
        /// 关闭UI
        /// </summary>
        /// <typeparam name="T">UIType</typeparam>
        public void CloseUI<T>() where T : UIBase
        {
            foreach (var v in AllPopUI)
            {
                if (v is T)
                {
                    AllPopUI.Remove(v);
                    break;
                }
            }
            UIKit.ClosePanel<T>();
        }

        /// <summary>
        /// 关闭UI
        /// </summary>
        /// <param name="ui">ui对象</param>
        public void CloseUI(UIBase ui)
        {
            foreach (var v in AllPopUI)
            {
                if (v == ui)
                {
                    AllPopUI.Remove(v);
                    break;
                }
            }
            UIKit.ClosePanel(ui);
        }

        /// <summary>
        /// 获取当前所有在Pop下的UI数量
        /// </summary>
        /// <returns></returns>
        public int GetAllPopUICount()
        {
            int count = 0;
            foreach (var v in AllPopUI)
            {
                if (v != null)
                    count++;
            }
            return count;
        }

        /// <summary>
        /// 关闭所有在Pop下的UI
        /// </summary>
        public void CloseAllPopUI()
        {
            int count = AllPopUI.Count;
            for (int i = 0; i < count; i++)
            {
                CloseUI(AllPopUI[0]);
            }
        }

        /// <summary>
        /// 隐藏UI
        /// </summary>
        /// <typeparam name="T">UIType</typeparam>
        public void HideUI<T>() where T : UIBase
        {
            UIKit.HidePanel<T>();
        }

        /// <summary>
        /// 重新展示UI
        /// </summary>
        /// <typeparam name="T">UIType</typeparam>
        public void ShowUI<T>() where T : UIBase
        {
            UIKit.ShowPanel<T>();
        }

        /// <summary>
        /// 隐藏所有UI
        /// </summary>
        public void HideAllUI()
        {
            UIKit.HideAllPanel();
        }

        /// <summary>
        /// 关闭所有UI
        /// </summary>
        public void CloseAllUI()
        {
            UIKit.CloseAllPanel();
            AllPopUI.Clear();
        }

        /// <summary>
        /// 获取UI
        /// </summary>
        /// <typeparam name="T">UIType</typeparam>
        /// <returns></returns>
        public UIBase GetUI<T>() where T : UIBase
        {
            return UIKit.GetPanel<T>();
        }

        public UIRoot GetUIRoot()
        {
            return UIKit.Root;
        }

        public Camera GetUICamera()
        {
            return UIKit.Root.Camera;
        }
    }
}
